<template>
    <transition name="fdx-zoom-out">
        <div class="fdx-tip fdx-tip-bar" ref="tip" v-show="visible">{{title}}</div>
    </transition>
</template>

<script>
const defaultUnitTip = '个楼盘'
export default {
    name: 'ResultTip',
    props: {
        text: {
            type: String,
            default: ''
        },
        num: [Number, String],
        duration: {
            type: Number,
            default: 3000
        },
        unit: {
            type: String,
            default: defaultUnitTip
        },
        prefix: {
            type: String,
            default: '为您找到'
        }
    },
    computed: {
        title: function() {
            if (this.num !== '') {
                let unitTip = (this.unit === null ? defaultUnitTip : this.unit)
                return `${this.prefix}${this.num}${unitTip}`
            }
            return this.text
        }
    },
    data() {
        return {
            visible: true
        }
    },
    mounted() {
        this.setTimer()
    },
    methods: {
        clearTimer() {
            clearTimeout(this.timer)
            this.timer = null
        },
        setTimer() {
            if (this.timer) {
                this.clearTimer()
            }
            this.timer = setTimeout(() => {
                this.visible = false
                this.$emit('close', false)
                this.timer = null
            }, this.duration)
        },
        show() {
            this.visible = true
            this.setTimer()
        }
    }
}
</script>

<style  lang="scss">
@import '../../assets/sass/_vars.scss';
.fdx-tip-bar {
    margin-top: 6px;
    height: 38px;
    line-height: 38px;
    text-align: center;
    font-size: 12px;
    color: #aaa;
    background-color: $bg-gray;
}
</style>